local XMPQT = XLGetGlobal("XMPQT")


XMPQT.Output = {}

function GetEditObject()
	local 	treeMananger = XLGetObject("Xunlei.UIEngine.TreeManager")
	local 	rootObjTree  = treeMananger:GetUIObjectTree("XMPQT.MainObjTree.Instance")

	local 	bkgObject 	= rootObjTree:GetUIObject("app.bkg")
	local 	editObj 	= bkgObject:GetObject("mainwnd.body:RichEditPanel:edit_simple")
	return 	editObj
end


if XMPQT.QTEvent ~= nil then
	XMPQT.QTEvent:addEventListener("OnTestCaseStart",InitVariable)
end
function XMPQT.Output.Title(text)
	if text == nil or type(text) ~= "string" then
		return
	end
	
	AsynCall(function()
		local editObj = GetEditObject()
		local fmtold , bret = editObj:GetTextCharFormat()
		local fmt = {textcolor="#000000",weight = 6000,height = 18}
		editObj:SetTextCharFormat(fmt)
		editObj:AppendText(text.."\r\n")
		editObj:SetTextCharFormat(fmtold)
		XMPQT.API:Call("WriteFile",XMPQT.LogFileName,text)
	end)

end

function Insert2Tree(text,state,parentoffset)
	if parentoffset == nil then parentoffset = 0 end
	local parent = nil
	local id = nil
	if XMPQT.TreeIdMap then 
		parent = XMPQT.tcIndex-parentoffset
		id = XMPQT.tcIndex*10000+XMPQT.TreeIdMap[XMPQT.tcIndex]
	end
	if XMPQT.TreeIdMap and parent and id then
		XMPQT.TreeIdMap[XMPQT.tcIndex] = XMPQT.TreeIdMap[XMPQT.tcIndex]+1
		XMPQT.Helper.InsertTreeItem(parent,id,text,state)
	end
end

function XMPQT.Output.Info(text,level)
	if level == nil then
		XMPQT.Output.Info2(text)
	elseif	level == 0 then
		XMPQT.Output.Error(text)
	elseif	level == 1 then
		XMPQT.Output.Success(text)
	elseif level == 2 then
		XMPQT.Output.Warning(text)
	end
	XMPQT.API:Call("TSInfo",1,text)
	 
end

function XMPQT.Output.Info2(text,bInsert2tree)
	if text == nil or type(text) ~= "string" then
		return
	end
	AsynCall(function()
		local editObj = GetEditObject()
		local fmtold , bret = editObj:GetTextCharFormat()
		local fmt = {textcolor="#000000"}
		editObj:SetTextCharFormat(fmt)
		editObj:AppendText(text.."\r\n")
		editObj:SetTextCharFormat(fmtold)
		if bInsert2tree== true or bInsert2tree == nil	then 
			Insert2Tree(text,0)
		end
		XMPQT.API:Call("WriteFile",XMPQT.LogFileName,text)
	end)

end

function XMPQT.Output.Warning(text)
	if text == nil or type(text) ~= "string" then
		return
	end
	
	AsynCall(function()
		local editObj = GetEditObject()
		local fmtold , bret = editObj:GetTextCharFormat()
		local fmt = {textcolor="#ff8000"}
		editObj:SetTextCharFormat(fmt)
		editObj:AppendText(text.."\r\n")
		editObj:SetTextCharFormat(fmtold)
		
		Insert2Tree(text,13)
		XMPQT.API:Call("WriteFile",XMPQT.LogFileName,text)
	end)
end

function XMPQT.Output.Error(text,offset)
	if text == nil or type(text) ~= "string" then
		return
	end
	
	AsynCall(function()
		local editObj = GetEditObject()
		local fmtold , bret = editObj:GetTextCharFormat()
		local fmt = {textcolor="#ff0000",weight = 6000,height = 16}
		editObj:SetTextCharFormat(fmt)
		editObj:AppendText(text.."\r\n")
		editObj:SetTextCharFormat(fmtold)
		Insert2Tree(text,12,offset)
		XMPQT.API:Call("WriteFile",XMPQT.LogFileName,text)
	end)
	
end

function XMPQT.Output.Success(text,offset)
	if text == nil or type(text) ~= "string" then
		return
	end
	
	AsynCall(function()
		local editObj = GetEditObject()
		local fmtold , bret = editObj:GetTextCharFormat()
		local fmt = {textcolor="#00ff00"}
		editObj:SetTextCharFormat(fmt)
		editObj:AppendText(text.."\r\n")
		editObj:SetTextCharFormat(fmtold)
		
		Insert2Tree(text,11,offset)
		XMPQT.API:Call("WriteFile",XMPQT.LogFileName,text)
	end)
	
	
end



